package com.jiadakeji.ppm.api.v1;

import com.jiadakeji.ppm.api.common.JsonResult;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.dao.RoomDao;
import com.jiadakeji.ppm.modules.business.entity.Room;
import com.jiadakeji.ppm.modules.business.service.RoomService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController("v1_roomController")
@RequestMapping(value = "${apiPath}/v1/room")
public class RoomController extends BaseController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomDao roomDao;

    @RequestMapping(value = "/list")
    @ApiOperation(value = "获取楼栋列表",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<List<Room>> list(@RequestBody Room room, HttpServletRequest request, HttpServletResponse response){
        JsonResult<List<Room>> result = new JsonResult<>();

        List<Room> roomList = roomService.findList(room);
        result.setData(roomList);
        return result;
    }

    @RequestMapping(value = "/getMyRoomList")
    @ApiOperation(value = "获取我的房间列表",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<List<Room>> getMyRoomList(@RequestBody Room room, HttpServletRequest request, HttpServletResponse response){
        JsonResult<List<Room>> result = new JsonResult<>();
        if(StringUtils.isBlank(room.getHouseHolderId())&&StringUtils.isBlank(room.getUsingCustomerId())){
            return result.setCodeAndMessage(-1,"所有者和使用者不能都为空");
        }
        List<Room> roomList = roomService.findList(room);
        result.setData(roomList);
        return result;
    }

    @RequestMapping(value = "/setRoomUsing")
    @ApiOperation(value = "获取我的房屋使用者",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<String> setRoomUsing(@RequestBody Room room, HttpServletRequest request, HttpServletResponse response){
        JsonResult<String> result = new JsonResult<>();
        if(StringUtils.isBlank(room.getId())){
            return result.setCodeAndMessage(-1,"id不能为空");
        }
        if(StringUtils.isBlank(room.getHouseHolderId())) {
            return result.setCodeAndMessage(-1,"houseHolderId 不能为空");
        }

        if( StringUtils.isBlank(room.getUsingCustomerId())){
             return result.setCodeAndMessage(-1,"usingCustomerId 不能为空");
        }
        Room r = roomService.get(room.getId());
        if(null ==r){
            return result.setCodeAndMessage(-1,"room不存在");
        }
        if(!r.getHouseHolderId().equals(room.getHouseHolderId())){
            return result.setCodeAndMessage(-1,"屋主信息不正确");
        }
        r.setUsingCustomerId(room.getUsingCustomerId());
        roomService.save(r);
        return result;
    }

    @RequestMapping(value = "/overMyRoom")
    @ApiOperation(value = "获取我的房屋使用者",httpMethod = "POST",consumes="application/json",notes="根据id")
    public JsonResult<String> overMyRoom(@RequestBody Room room, HttpServletRequest request, HttpServletResponse response){
        JsonResult<String> result = new JsonResult<>();
        if(StringUtils.isBlank(room.getId())){
            return result.setCodeAndMessage(-1,"id不能为空");
        }
        if(StringUtils.isBlank(room.getHouseHolderId())) {
            return result.setCodeAndMessage(-1,"houseHolderId 不能为空");
        }

        Room r = roomService.get(room.getId());
        if(null ==r){
            return result.setCodeAndMessage(-1,"room不存在");
        }
        if(!r.getHouseHolderId().equals(room.getHouseHolderId())){
            return result.setCodeAndMessage(-1,"屋主信息不正确");
        }
       roomDao.delHouseHolder(room.getId());
        return result;
    }

}
